home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / PlayerPRO 4.5.8 / PlayerPRO 4.5.8 Dev.Kit / Plug-Ins / Instruments Import⁄Export Plugs / XI.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-05  |  13.1 KB  |  536 lines  |  [TEXT/CWIE]

  1. /*    XI                */
  2. /*  IMPORT/EXPORT    */
  3. /*    v 1.0            */
  4. /*    1996 by ANR        */
  5.  
  6. #include "PPPlug.h"
  7. #include "sound.h"
  8.  
  9. #if defined(powerc) || defined(__powerc)
  10. enum {
  11.         PlayerPROPlug = kCStackBased
  12.         | RESULT_SIZE(SIZE_CODE( sizeof(OSErr)))
  13.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( OSType)))
  14.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( InstrData*)))
  15.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( sData**)))
  16.         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( short*)))
  17.         | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( FSSpec*)))
  18.         | STACK_ROUTINE_PARAMETER(6, SIZE_CODE(sizeof( PPInfoPlug*)))
  19. };
  20.  
  21. ProcInfoType __procinfo = PlayerPROPlug;
  22. #else
  23. #include <A4Stuff.h>
  24. #endif
  25.  
  26. #if defined(powerc) || defined(__powerc)
  27. #pragma options align=mac68k
  28. #endif
  29.  
  30. #define UWORD unsigned short
  31. #define ULONG unsigned long
  32. #define UBYTE Byte
  33. #define BYTE char
  34. #define BOOL Boolean
  35.  
  36. #define HEADERSIZE     276
  37. #define PHSIZE        9
  38. #define IHSIZE        263
  39. #define IHSIZESMALL    33
  40. #define IHSSIZE        40
  41.  
  42. typedef struct XMPATCHHEADER{
  43.     
  44.     UBYTE what[96];         // (byte) Sample number for all notes
  45.     UWORD volenv[24];       // (byte) Points for volume envelope
  46.     UBYTE panenv[48];       // (byte) Points for panning envelope
  47.     UBYTE volpts;           // (byte) Number of volume points
  48.     UBYTE panpts;           // (byte) Number of panning points
  49.     UBYTE volsus;           // (byte) Volume sustain point
  50.     UBYTE volbeg;           // (byte) Volume loop start point
  51.     UBYTE volend;           // (byte) Volume loop end point
  52.     UBYTE pansus;           // (byte) Panning sustain point
  53.     UBYTE panbeg;           // (byte) Panning loop start point
  54.     UBYTE panend;           // (byte) Panning loop end point
  55.     UBYTE volflg;           // (byte) Volume type: bit 0: On; 1: Sustain; 2: Loop
  56.     UBYTE panflg;           // (byte) Panning type: bit 0: On; 1: Sustain; 2: Loop
  57.     UBYTE vibflg;           // (byte) Vibrato type
  58.     UBYTE vibsweep;         // (byte) Vibrato sweep
  59.     UBYTE vibdepth;         // (byte) Vibrato depth
  60.     UBYTE vibrate;          // (byte) Vibrato rate
  61.     UWORD volfade;          // (word) Volume fadeout
  62.     UWORD reserved[11];     // (word) Reserved
  63. } XMPATCHHEADER;
  64.  
  65.  
  66. typedef struct XMWAVHEADER{
  67.     ULONG length;           // (dword) Sample length
  68.     ULONG loopstart;        // (dword) Sample loop start
  69.     ULONG looplength;       // (dword) Sample loop length
  70.     UBYTE volume;           // (byte) Volume
  71.     BYTE finetune;          // (byte) Finetune (signed byte -128..+127)
  72.     UBYTE type;                     // (byte) Type: Bit 0-1: 0 = No loop, 1 = Forward loop,
  73.                                     // 2 = Ping-pong loop;
  74.                                     // 4: 16-bit sampledata
  75.     UBYTE panning;          // (byte) Panning (0-255)
  76.     BYTE  relnote;          // (byte) Relative note number (signed byte)
  77.     UBYTE reserved;         // (byte) Reserved
  78.     char  samplename[22];   // (char) Sample name
  79. } XMWAVHEADER;
  80.  
  81. #if defined(powerc) || defined(__powerc)
  82. #pragma options align=reset
  83. #endif
  84.  
  85. OSErr TestXI( Ptr CC)
  86. {
  87.     if( *((OSType*) CC) == 'Exte') return noErr;
  88.     else return MADFileNotSupportedByThisPlug;
  89. }
  90.  
  91. OSErr MAD2KillInstrument( InstrData *curIns, sData **sample)
  92. {
  93. short            i;
  94. Boolean            IsReading;
  95.  
  96.     for( i = 0; i < curIns->numSamples; i++)
  97.     {
  98.         if( sample[ i] != 0L)
  99.         {
  100.             if( sample[ i]->data != 0L)
  101.             {
  102.                 DisposPtr( (Ptr) sample[ i]->data);
  103.                 sample[ i]->data = 0L;
  104.             }
  105.             DisposPtr( (Ptr) sample[ i]);
  106.             sample[ i] = 0L;
  107.         }
  108.     }
  109.     
  110.     
  111.     for( i = 0; i < 32; i++) curIns->name[ i]    = 0;
  112.     curIns->type        = 0;
  113.     curIns->numSamples    = 0;
  114.     
  115.     /**/
  116.     
  117.     for( i = 0; i < 96; i++) curIns->what[ i]        = 0;
  118.     for( i = 0; i < 12; i++)
  119.     {
  120.         curIns->volEnv[ i].pos        = 0;
  121.         curIns->volEnv[ i].val        = 0;
  122.     }
  123.     for( i = 0; i < 12; i++)
  124.     {
  125.         curIns->pannEnv[ i].pos    = 0;
  126.         curIns->pannEnv[ i].val    = 0;
  127.     }
  128.     curIns->volSize        = 0;
  129.     curIns->pannSize    = 0;
  130.     
  131.     curIns->volSus        = 0;
  132.     curIns->volBeg        = 0;
  133.     curIns->volEnd        = 0;
  134.     
  135.     curIns->pannSus        = 0;
  136.     curIns->pannBeg        = 0;
  137.     curIns->pannEnd        = 0;
  138.  
  139.     curIns->volType        = 0;
  140.     curIns->pannType    = 0;
  141.     
  142.     curIns->volFade        = 0;
  143.     curIns->vibDepth    = 0;
  144.     curIns->vibRate        = 0;
  145.     
  146.     return noErr;
  147. }
  148.  
  149. unsigned long Tdecode32( void *msg_buf)
  150. {
  151.   unsigned char *buf = msg_buf;
  152.   
  153.   return( (unsigned long) buf[3] << 24) | ( (unsigned long) buf[2] << 16) | ( (unsigned long) buf[ 1] << 8) | ( (unsigned long) buf[0]);
  154. }
  155.  
  156. short Tdecode16( void *msg_buf)
  157. {
  158.   unsigned char *buf = msg_buf;
  159.   
  160.   return ( (short) buf[1] << 8) | ( (short) buf[0]);
  161. }
  162.  
  163. OSErr main(        OSType                    order,                        // Order to execute
  164.                 InstrData                *InsHeader,                    // Ptr on instrument header
  165.                 sData                    **sample,                    // Ptr on samples data
  166.                 short                    *sampleID,                    // If you need to replace/add only a sample, not replace the entire instrument (by example for 'AIFF' sound)
  167.                                                                     // If sampleID == -1 : add sample else replace selected sample.
  168.                 FSSpec                    *AlienFileFSSpec,            // IN/OUT file
  169.                 PPInfoPlug                *thePPInfoPlug)
  170. {
  171.     OSErr    myErr;
  172.     short    iFileRefI, x;
  173.     long    inOutCount;
  174.     
  175.     #ifndef powerc
  176.         long    oldA4 = SetCurrentA4();             //this call is necessary for strings in 68k code resources
  177.     #endif
  178.     
  179.     switch( order)
  180.     {
  181.         case 'IMPL':
  182.         {
  183.             Ptr                theXI;
  184.             XMPATCHHEADER    *pth;
  185.             XMWAVHEADER        *wh;
  186.             short            numSamples;
  187.             
  188.             myErr = FSpOpenDF( AlienFileFSSpec, fsCurPerm, &iFileRefI);
  189.             if( myErr == noErr)
  190.             {
  191.                 GetEOF( iFileRefI, &inOutCount);
  192.                 
  193.                 theXI = NewPtr( inOutCount);
  194.                 if( theXI == 0L) myErr = MADNeedMemory;
  195.                 else
  196.                 {
  197.                     MAD2KillInstrument( InsHeader, sample);
  198.                     
  199.                     for( x = 0; x < 32; x++)
  200.                     {
  201.                         if( x < AlienFileFSSpec->name[ 0]) InsHeader->name[ x] = AlienFileFSSpec->name[ x + 1];
  202.                         else InsHeader->name[ x] = '\0';
  203.                     }
  204.                     
  205.                     GetEOF( iFileRefI, &inOutCount);
  206.                     
  207.                     myErr = FSRead( iFileRefI, &inOutCount, theXI);
  208.                     
  209.                     // READ instrument header
  210.                     
  211.                     pth = (XMPATCHHEADER*) (theXI + 0x42);
  212.                     
  213.                     numSamples = *((short*) (theXI + 0x42 + sizeof( XMPATCHHEADER)));
  214.                     InsHeader->numSamples = Tdecode16( &numSamples);
  215.                     
  216.                     pth->volfade     = Tdecode16( &pth->volfade);
  217.                     
  218.                     BlockMove( pth->what,         InsHeader->what,     96);
  219.                     BlockMove( pth->volenv,     InsHeader->volEnv,     48);
  220.                     for( x = 0; x < 12; x++)
  221.                     {
  222.                         InsHeader->volEnv[ x].pos = Tdecode16( &InsHeader->volEnv[ x].pos);    // 
  223.                         InsHeader->volEnv[ x].val = Tdecode16( &InsHeader->volEnv[ x].val);    // 00...64
  224.                     }
  225.                     
  226.                     InsHeader->volSize    = pth->volpts;
  227.                     InsHeader->volType    = pth->volflg;
  228.                     InsHeader->volSus    = pth->volsus;
  229.                     InsHeader->volBeg    = pth->volbeg;
  230.                     InsHeader->volEnd    = pth->volend;
  231.                     InsHeader->volFade    = pth->volfade;
  232.                     
  233.                     BlockMove( pth->panenv, InsHeader->pannEnv,     48);
  234.                     
  235.                     // Read SAMPLE HEADERS
  236.                     
  237.                     for( x = 0; x < InsHeader->numSamples; x++)
  238.                     {
  239.                         sData    *curData;
  240.                         long    size, i;
  241.                         long     finetune[ 16] = 
  242.                         {
  243.                             7895,    7941,    7985,    8046,    8107,    8169,    8232,    8280,
  244.                             8363,    8413,    8463,    8529,    8581,    8651,    8723,    8757
  245.                         };
  246.                         
  247.                         wh = (XMWAVHEADER*) (theXI + 0x42 + 0x02 + sizeof( XMPATCHHEADER) + x * sizeof( XMWAVHEADER));
  248.                         
  249.                         wh->length         = Tdecode32( &wh->length);
  250.                         wh->loopstart     = Tdecode32( &wh->loopstart);
  251.                         wh->looplength     = Tdecode32( &wh->looplength);
  252.                         
  253.                         ///////////
  254.                         
  255.                         curData = sample[ x] = MADCreateSample();
  256.                         
  257.                         curData->size        = wh->length;
  258.                         curData->loopBeg     = wh->loopstart;
  259.                         curData->loopSize     = wh->looplength;
  260.                         
  261.                         curData->vol        = wh->volume;
  262.                         curData->c2spd        = finetune[ (wh->finetune + 128)/16];
  263.                         curData->loopType    = 0;
  264.                         curData->amp        = 8;
  265.                         if( wh->type & 0x10)        // 16 Bits
  266.                         {
  267.                             curData->amp = 16;
  268.                         }
  269.                         if( !(wh->type & 0x3))
  270.                         {
  271.                             curData->loopBeg = 0;
  272.                             curData->loopSize = 0;
  273.                         }
  274.                         
  275.                         curData->panning    = wh->panning;
  276.                         curData->relNote    = wh->relnote;
  277.                         for( i = 0; i < 22; i++) curData->name[ i] = wh->samplename[ i];
  278.                     }
  279.                     
  280.                     // Read SAMPLE DATA
  281.                     {
  282.                         Ptr reader = (Ptr) wh;
  283.                         
  284.                         reader += sizeof( XMWAVHEADER);
  285.                         
  286.                         for( x = 0; x < InsHeader->numSamples; x++)
  287.                         {
  288.                             sData *curData = sample[ x];
  289.                             
  290.                             curData->data = NewPtr( curData->size);
  291.                             if( curData->data != 0L)
  292.                             {
  293.                                 BlockMove( reader, curData->data, curData->size);
  294.                                 
  295.                                 if( curData->amp == 16)
  296.                                 {
  297.                                     short    *tt;
  298.                                     long    tL;
  299.                                     
  300.                                     tt = (short*) curData->data;
  301.  
  302.                                     for( tL = 0; tL < curData->size/2; tL++)
  303.                                     {
  304.                                         *(tt + tL) = Tdecode16( (Ptr) (tt + tL));
  305.                                     }
  306.                                     
  307.                                     {
  308.                                     /* Delta to Real */
  309.                                     long    oldV, newV;
  310.                                     long    xL;
  311.                                     
  312.                                     oldV = 0;
  313.                                     
  314.                                     for( xL = 0; xL < curData->size/2; xL++)
  315.                                     {
  316.                                         newV = tt[ xL] + oldV;
  317.                                         tt[ xL] = newV;
  318.                                         oldV = newV;
  319.                                     }
  320.                                     }
  321.                                 }
  322.                                 else
  323.                                 {
  324.                                     /* Delta to Real */
  325.                                     long    oldV, newV;
  326.                                     long    xL;
  327.                                     
  328.                                     oldV = 0;
  329.  
  330.                                     for( xL = 0; xL < curData->size; xL++)
  331.                                     {
  332.                                         newV = curData->data[ xL] + oldV;
  333.                                         curData->data[ xL] = newV;
  334.                                         oldV = newV;
  335.                                     }
  336.                                 }
  337.                             }
  338.                             
  339.                             reader += curData->size;
  340.                         }
  341.                     }
  342.                 }
  343.                 
  344.                 FSClose( iFileRefI);
  345.             }
  346.         }
  347.         break;
  348.         
  349.         case 'TEST':
  350.         {
  351.             Ptr    theSound;
  352.             
  353.             myErr = FSpOpenDF( AlienFileFSSpec, fsCurPerm, &iFileRefI);
  354.             if( myErr == noErr)
  355.             {
  356.                 inOutCount = 50L;
  357.                 theSound = NewPtr( inOutCount);
  358.                 if( theSound == 0L) myErr = MADNeedMemory;
  359.                 else
  360.                 {
  361.                     FSRead( iFileRefI, &inOutCount, theSound);
  362.                     
  363.                     myErr = TestXI( (Ptr) theSound);
  364.                 }
  365.                 
  366.                 DisposPtr( theSound);
  367.                 
  368.                 FSClose( iFileRefI);
  369.             }
  370.         }
  371.         break;
  372.         
  373.         case 'EXPL':
  374.             myErr = FSpCreate( AlienFileFSSpec, 'SNPL', 'XI  ', smCurrentScript);
  375.             myErr = FSpOpenDF( AlienFileFSSpec, fsCurPerm, &iFileRefI);
  376.             
  377.             if( myErr == noErr)
  378.             {
  379.                 // Write instrument header
  380.                 
  381.                 short            u, v, p, i, x;
  382.                 long            inOutCount = 0;
  383.                 long            ihsizecopy, ihssizecopy;
  384.                 XMPATCHHEADER    pth;
  385.                 char            start[ 0x42];
  386.                 
  387.                 BlockMove( "Extended Instrument:                       FastTracker v2.00   ", start, 0x42);
  388.                 
  389.                 inOutCount = 0x42;
  390.                 FSWrite( iFileRefI, &inOutCount, start);
  391.                 
  392.                 BlockMove( InsHeader->what, pth.what, 96);
  393.                 BlockMove( InsHeader->volEnv, pth.volenv, 48);
  394.                 for( x = 0; x < 24; x++)
  395.                 {
  396.                     pth.volenv[ x] = Tdecode16( &pth.volenv[ x]);
  397.                 }
  398.                 
  399.                 pth.volpts = InsHeader->volSize;
  400.                 pth.volflg = InsHeader->volType;
  401.                 pth.volsus = InsHeader->volSus;
  402.                 pth.volbeg = InsHeader->volBeg;
  403.                 pth.volend = InsHeader->volEnd;
  404.                 pth.volfade = InsHeader->volFade;
  405.                 
  406.                 BlockMove( InsHeader->pannEnv, pth.panenv, 48);
  407.                 
  408.                 pth.volfade     = Tdecode16( &pth.volfade);
  409.                 
  410.                 inOutCount = sizeof( XMPATCHHEADER);
  411.                 FSWrite( iFileRefI, &inOutCount, &pth);
  412.                 
  413.                 inOutCount = 2;
  414.                 x = InsHeader->numSamples;
  415.                 x = Tdecode16( &x);
  416.                 FSWrite( iFileRefI, &inOutCount, &x);
  417.                 
  418.                 /** WRITE samples */
  419.                 
  420.                 for( u = 0 ; u < InsHeader->numSamples ; u++)
  421.                 {
  422.                     XMWAVHEADER        wh;
  423.                     sData            *curData;
  424.                     long     finetune[ 16] = 
  425.                     {
  426.                         7895,    7941,    7985,    8046,    8107,    8169,    8232,    8280,
  427.                         8363,    8413,    8463,    8529,    8581,    8651,    8723,    8757
  428.                     };
  429.                     
  430.                     curData = sample[ u];
  431.                     
  432.                     wh.length = curData->size;        inOutCount += wh.length;
  433.                     
  434.                     wh.loopstart    = curData->loopBeg;
  435.                     wh.looplength    = curData->loopSize;
  436.                     wh.volume        = curData->vol;
  437.                     
  438.                     wh.finetune = -128;
  439.                     if( curData->c2spd > 8757) wh.finetune = 127;
  440.                     else
  441.                     {
  442.                         while( finetune[ (wh.finetune + 128)/16] < curData->c2spd)
  443.                         {
  444.                             wh.finetune += 16;
  445.                         }
  446.                     }
  447.                     wh.type = 0;
  448.                     if( curData->amp == 16) wh.type += 0x10;
  449.                     if( curData->loopSize > 0) wh.type += 0x3;
  450.                     
  451.                     wh.panning = curData->panning;
  452.                     wh.relnote = curData->relNote;
  453.                     for( x = 0; x < 22; x++) wh.samplename[ x] = curData->name[ x];
  454.                     
  455.                     wh.length         = Tdecode32( &wh.length);
  456.                     wh.loopstart     = Tdecode32( &wh.loopstart);
  457.                     wh.looplength     = Tdecode32( &wh.looplength);
  458.                     
  459.                     inOutCount = sizeof( wh);
  460.                     FSWrite( iFileRefI, &inOutCount, &wh);
  461.                 }
  462.                 
  463.                 for( u = 0 ; u < InsHeader->numSamples ; u++)
  464.                 {
  465.                     sData     *curData = sample[ u];
  466.                     Ptr        tempPtr;
  467.                     
  468.                     tempPtr = NewPtr( curData->size);
  469.                         
  470.                     /// WriteData
  471.                     if( tempPtr != 0L)
  472.                     {
  473.                         BlockMove( curData->data, tempPtr, curData->size);
  474.                         
  475.                         if( curData->amp == 16)
  476.                         {
  477.                             short    *tt = (short*) tempPtr;
  478.                             long    tL;
  479.                             
  480.                             /* Real to Delta */
  481.                             long    oldV, newV;
  482.                             long    xL;
  483.                             
  484.                             oldV = 0;
  485.                             
  486.                             for( xL = 0; xL < curData->size/2; xL++)
  487.                             {
  488.                                 newV = tt[ xL];
  489.                                 tt[ xL] -= oldV;
  490.                                 oldV = newV;
  491.                             }
  492.                             
  493.                             for( tL = 0; tL < curData->size/2; tL++)
  494.                             {
  495.                                 *(tt + tL) = Tdecode16( (Ptr) (tt + tL));
  496.                             }
  497.                         }
  498.                         else
  499.                         {
  500.                             /* Real to Delta */
  501.                             long    oldV, newV;
  502.                             long    xL;
  503.                             Ptr        tt = ( Ptr) tempPtr;
  504.                             
  505.                             oldV = 0;
  506.                             
  507.                             for( xL = 0; xL < curData->size; xL++)
  508.                             {
  509.                                 newV = tt[ xL];
  510.                                 tt[ xL] -= oldV;
  511.                                 oldV = newV;
  512.                             }
  513.                         }
  514.                         
  515.                         inOutCount = curData->size;
  516.                         FSWrite( iFileRefI, &inOutCount, tempPtr);
  517.                         
  518.                         DisposePtr( tempPtr);
  519.                     }
  520.                 }
  521.                 
  522.                 FSClose( iFileRefI);
  523.             }
  524.         break;
  525.         
  526.         default:
  527.             myErr = MADOrderNotImplemented;
  528.         break;
  529.     }
  530.     
  531.     #ifndef powerc
  532.         SetA4( oldA4);
  533.     #endif
  534.     
  535.     return myErr;
  536. }